package com.yanqu.road.server.gameplayer.module.activity.horserun.event;

import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;
import com.yanqu.road.entity.activity.horserun.config.HorseRunEventConfig;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *    card("翻牌子事件",10),
 */
public class HorseRunCardEvent extends HorseRunBaseEvent {

    @Override
    public HorseRunProto.EventResult.Builder meet(HorseRunUserGamingData userGamingData, HorseRunConfig config, HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();
        userGamingData.setCardEventMap(new HashMap<>());
        builder.setError(0);
        //加到日志里面
        addUserLog(userGamingData,config,1,eventId,new ArrayList<>(),builder);
        //进行中
        eventGoing(userGamingData);
        return builder;
    }

    @Override
    public HorseRunProto.EventResult.Builder deal(HorseRunUserGamingData userGamingData, HorseRunDealEventArg arg, HorseRunConfig config, HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();

        int cardPosition = arg.getCardEventArg();
        if (cardPosition <=0){
            builder.setError(GameErrorCode.E_HORSE_RUN_ARG_ERROR);
            return builder;
        }


        Map<Integer, Integer> cardEventMap = userGamingData.getCardEventMap();

        if (cardEventMap.containsKey(cardPosition)){
            builder.setError(GameErrorCode.E_HORSE_RUN_CARD_HAS_OPEN);
            return builder;
        }

        builder.setError(0);

        int cardId = config.randomCardEventCard(eventConfigById);

        HorseRunProto.HorseRunCard.Builder cardBuild = HorseRunProto.HorseRunCard.newBuilder();
        cardBuild.setCard(cardId);
        cardBuild.setPosition(cardPosition);

        builder.setCardEventChoose(cardBuild);

        cardEventMap.put(cardPosition,cardId);

        Map<Integer,Integer> existMap = new HashMap<>();

        int resultCardId = 0;

        for (int card:cardEventMap.values()){
            if (existMap.containsKey(card)){
                existMap.put(card,existMap.get(card)+1);
                if (existMap.get(card)>=3){
                    resultCardId = card;
                    break;
                }
            }else {
                existMap.put(card,1);
            }
        }

        if (resultCardId == 0){
            //进行中
            eventGoing(userGamingData);
        }else {
            List<Integer> paramList = eventConfigById.getParamList();
            int expAdd = paramList.get(resultCardId - 1)*userGamingData.getOpenGameMultiple();
            userGamingData.addExpSum(expAdd);
            builder.setExpAdd(expAdd);

            List<Integer> logParamList = new ArrayList<>();
            logParamList.add(resultCardId);
            logParamList.add(expAdd);

            addUserLog(userGamingData,config,2,eventId,logParamList,builder);
            eventDone(userGamingData);
        }
        return builder;
    }
}
